/*
 * @lc app=leetcode.cn id=1 lang=c
 *
 * [1] 两数之和
 */

// @lc code=start
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define NO -1

int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
    int i, j;
    int k = 0;
    int flag = 0;
    int *temp;
    temp = (int *)malloc(numsSize * sizeof(int));

    for (i = 0; i < numsSize; i++)
    {
        for (j = numsSize - 1; j > i; j--)
        {
            if (nums[i] + nums[j] == target)
            {
                temp[k] = i;
                k++;
                temp[k] = j;
                k++;
                *returnSize = *returnSize + 2;
            }
        }
    }

    if (0 == k)
    {
        *returnSize = 0;
        return NULL;
    }
    else
    {
        return temp;
    }
}

int main()
{
    int i, numsSize, target;
    int *num = (int *)malloc(sizeof(int));
    int *nums = (int *)malloc(sizeof(int));
    int *returnSize = (int *)malloc(sizeof(int));
    ;

    printf("请输入要生成的的数组的长度:");
    scanf("%d", &numsSize);

    srand((unsigned)time(NULL));

    for (i = 0; i < numsSize; i++)
    {
        nums[i] = rand() % 20 + 1;
    }

    printf("生成的数组为：\n");
    for (i = 0; i < numsSize; i++)
    {
        printf("%-2d ", nums[i]);
    }
    printf("\n");

    printf("请输入两数之和的目标数( < 40)：");
tar:
    scanf("%d", &target);

    if (target > 40)
    {
        printf("请重新输入，数据过大！\n");
        goto tar;
    }

    num = twoSum(nums, numsSize, target, returnSize);

    if (NULL == num)
    {
        printf("数组中没有任何两个数满足条件！");
    }
    else
    {
        printf("满足条件的数组序号有：");
        for (i = 0; i < *returnSize; i++)
        {
            if (i % 2 == 0)
            {
                printf("\n");
            }
            printf("%-2d ", num[i]);
        }
    }
    printf("\n");
}
